{$CLEO .cs}
{$USE CLEO+}
{$USE newOpcodes}
{$USE ini}
{$USE Audio}

0000: NOP

script_name "Radio Effects"

// By ArtemQa146

const
    progress = 30@
    radio_volume = 31@
    radio_SFX = 29@
    radio_time = 28@
    volume_1 = 27@
    volume_NOISE_INI = 26@
    volume_DAMAGE_INI = 25@
    Damage_level_INI = 24@
    Time_brouke = 23@
end

0AF2: volume_NOISE_INI = read_float_from_ini_file "cleo\Radio Effects.ini" section "Settings" key "Volume Noise"
0AF2: volume_DAMAGE_INI = read_float_from_ini_file "cleo\Radio Effects.ini" section "Settings" key "Volume Damage"
0AF2: Damage_level_INI = read_float_from_ini_file "cleo\Radio Effects.ini" section "Settings" key "Impact intensity"
0AF0: Time_brouke = read_int_from_ini_file "cleo\Radio Effects.ini" section "Settings" key "Maximum time impact SFX"

while true
wait 0
    if
        player.Defined(0)
    then
        if
            actor.Driving($player_actor)
        then
            051E: 2@ = get_current_radio_station
            0A8D: 1@ = read_memory 0xB5FCC8 size 4 virtual_protect 0
            if or
                not $ACTIVE_INTERIOR == 0
                2@ == 12
                0E4A: is_char_exiting_any_car $PLAYER_ACTOR
                1@ == 0.0
            then
                gosub @remove_audio
                continue
            end

            0@ = Actor.CurrentCar($PLAYER_ACTOR)
            
            0A97: 1@ = vehicle 0@ struct
            0D4E: 1@ = struct 1@ offset 0x1D3 size 1
            if
                not 1@ == 0
            then
                gosub @remove_audio
                continue
            end
            
            0E12: get_vehicle 0@ subclass_to 1@
            if and
                not 1@ == 0
                not 1@ == 4
                not 1@ == 1
                not 1@ == 2
                not 1@ == 9
            then
                gosub @remove_audio
                continue
            end
            
            0E65: get_car_collision_intensity 0@ store_to 1@
            if and
                progress == 0
                0025:   1@ > Damage_level_INI  // (float)
            then
                gosub @Broke
            end
                
            if and
                001D:   timera > radio_time  // (int)
                progress == 1
            then
                gosub @remove_audio
            end
            
            0407: store_coords_to 7@ 8@ 9@ from_car 0@ with_offset 0.0 1.0 2.0
            0407: store_coords_to 10@ 11@ 12@ from_car 0@ with_offset 0.0 20.0 20.0
            0407: store_coords_to 19@ 20@ 21@ from_car 0@ with_offset 0.0 -1.0 2.0
            0407: store_coords_to 16@ 17@ 18@ from_car 0@ with_offset 0.0 -20.0 20.0
            0A96: 15@ = ped $player_actor struct
            get_label_pointer 14@ = label @Buffer // 128
            if and
                0D3A: get_collision_between_points 7@ 8@ 9@ and 10@ 11@ 12@ flags 1 0 0 0 0 0 0 0 ignore_entity 15@ store_point_to 7@ 8@ 9@ entity_to 13@ colpoint_data_to 14@
                0D3A: get_collision_between_points 19@ 20@ 21@ and 16@ 17@ 18@ flags 1 0 0 0 0 0 0 0 ignore_entity 15@ store_point_to 7@ 8@ 9@ entity_to 13@ colpoint_data_to 14@
            then
                0407: store_coords_to 7@ 8@ 9@ from_car 0@ with_offset 0.0 20.0 2.0
                0407: store_coords_to 10@ 11@ 12@ from_car 0@ with_offset 20.0 20.0 2.0
                0407: store_coords_to 19@ 20@ 21@ from_car 0@ with_offset 0.0 30.0 2.0
                0407: store_coords_to 16@ 17@ 18@ from_car 0@ with_offset -20.0 30.0 2.0
                0A96: 15@ = ped $player_actor struct
                if and
                    0D3A: get_collision_between_points 7@ 8@ 9@ and 10@ 11@ 12@ flags 1 0 0 0 0 0 0 0 ignore_entity 15@ store_point_to 7@ 8@ 9@ entity_to 13@ colpoint_data_to 14@
                    0D3A: get_collision_between_points 19@ 20@ 21@ and 16@ 17@ 18@ flags 1 0 0 0 0 0 0 0 ignore_entity 15@ store_point_to 7@ 8@ 9@ entity_to 13@ colpoint_data_to 14@
                then
                    0407: store_coords_to 7@ 8@ 9@ from_car 0@ with_offset 0.0 -20.0 2.0
                    0407: store_coords_to 10@ 11@ 12@ from_car 0@ with_offset 20.0 -20.0 2.0
                    0407: store_coords_to 19@ 20@ 21@ from_car 0@ with_offset 0.0 -30.0 2.0
                    0407: store_coords_to 16@ 17@ 18@ from_car 0@ with_offset -20.0 -30.0 2.0
                    0A96: 15@ = ped $player_actor struct
                    if and
                        0D3A: get_collision_between_points 7@ 8@ 9@ and 10@ 11@ 12@ flags 1 0 0 0 0 0 0 0 ignore_entity 15@ store_point_to 7@ 8@ 9@ entity_to 13@ colpoint_data_to 14@
                        0D3A: get_collision_between_points 19@ 20@ 21@ and 16@ 17@ 18@ flags 1 0 0 0 0 0 0 0 ignore_entity 15@ store_point_to 7@ 8@ 9@ entity_to 13@ colpoint_data_to 14@
                    then
                        if
                            progress == 0
                        then
                            gosub @Noise
                        end
                        if
                            progress == 2
                        then
                            volume_1 -= 0.01
                            if volume_1 < 0.05
                            then 0208: volume_1 = random_float_in_ranges 0.05 0.4
                            end
                            0A8C: write_memory 0xB5FCC8 size 4 value volume_1 virtual_protect 1
                            0087: 1@ = volume_NOISE_INI // (float)
                            0063: 1@ -= volume_1  // (float)
                            if 1@ < 0.0
                            then 1@ = 0.0
                            end
                            0ABC: set_audio_stream radio_SFX volume 1@
                        end
                    end
                end
            else
                if
                    progress == 2
                then
                    volume_1 += 0.01
                    if volume_1 >= 1.0
                    then volume_1 = 1.0
                    end
                    0A8C: write_memory 0xB5FCC8 size 4 value volume_1 virtual_protect 1
                    0087: 1@ = volume_NOISE_INI // (float)
                    0063: 1@ -= volume_1  // (float)
                    if 1@ < 0.0
                    then 1@ = 0.0
                    end
                    0ABC: set_audio_stream radio_SFX volume 1@
                    if
                        volume_1 == 1.0
                    then
                        gosub @remove_audio
                    end
                else
                    volume_1 = 1.0
                end
            end
        else
            gosub @remove_audio
        end
    end
end

:Broke
progress = 1
timera = 0
0A8D: radio_volume = read_memory 0xB5FCC8 size 4 virtual_protect 0
0A8C: write_memory 0xB5FCC8 size 4 value 0.2 virtual_protect 1
0209: 1@ = random_int_in_ranges 1 3
if 1@ == 1
then 0AAC: radio_SFX = load_audio_stream "Radio SFX\broke.mp3"
else 0AAC: radio_SFX = load_audio_stream "Radio SFX\broke2.mp3"
end
0AAD: set_audio_stream radio_SFX state 1
0ABC: set_audio_stream radio_SFX volume volume_DAMAGE_INI
0AC0: audio_stream radio_SFX looped 1
0209: radio_time = random_int_in_ranges 2000 Time_brouke
return

:Noise
progress = 2
0A8D: radio_volume = read_memory 0xB5FCC8 size 4 virtual_protect 0
0A8C: write_memory 0xB5FCC8 size 4 value 0.2 virtual_protect 1
0AAC: radio_SFX = load_audio_stream "Radio SFX\noise.mp3"
0AAD: set_audio_stream radio_SFX state 1
0AC0: audio_stream radio_SFX looped 1
0ABC: set_audio_stream radio_SFX volume 0.0
timerb = 0
return

:remove_audio
if
    progress > 0
then
    progress = 0
    0A8C: write_memory 0xB5FCC8 size 4 value radio_volume virtual_protect 1
    if not radio_SFX == -1
    then 0AAE: remove_audio_stream radio_SFX
    end
    radio_SFX = -1
end
return

:Buffer
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP
0000: NOP